package com.troll.wanandroid.repo

import com.rxlife.coroutine.RxLifeScope
import com.troll.eth.live.Live
import com.troll.wanandroid.constant.ApiConstant
import com.troll.wanandroid.model.Article
import com.troll.wanandroid.model.ArticleIntroduction
import com.troll.wanandroid.model.BannerBean
import com.troll.wanandroid.net.Response
import rxhttp.toClass
import rxhttp.wrapper.param.RxHttp

/**
 *
 * @Author:   TangPeng
 * @Time:     6/1/21
 * @Remark:  Home的repo
 */
class HomeRepo {

    var liveArticle = Live<MutableList<ArticleIntroduction>>()
    var liveBanner = Live<MutableList<BannerBean>>()
    var liveError = Live<Boolean>()
    fun getArticle(rxLifeScope: RxLifeScope) {
        rxLifeScope.launch({
            liveArticle.value = RxHttp.get("/article/list/0/json")
                .toClass<Response<Article>>()
                .await().data?.datas
        }, onError = {
            liveError.value = true
        })
    }

    /**
     * 首页bannder
     */
    fun getBanner(rxLifeScope: RxLifeScope) {
        rxLifeScope.launch {
            liveBanner.value = RxHttp.get(ApiConstant.BANNER)
                .toClass<Response<MutableList<BannerBean>>>()
                .await().data
        }
    }
}